home *** CD-ROM | disk | FTP | other *** search
- Path: news.infi.net!usenet
- From: nngis@norfolk.infi.net (Greg DiGiorgio)
- Newsgroups: comp.lang.c
- Subject: Re: Overwriting file information?
- Date: 18 Jan 1996 14:37:21 GMT
- Organization: Customer of InfiNet
- Message-ID: <4dllv1$7af@news.infi.net>
- References: <Ed-1601960129180001@allentown03.voicenet.com>
- Reply-To: nngis@norfolk.infi.net
- NNTP-Posting-Host: h-hengest.norfolk.infi.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <Ed-1601960129180001@allentown03.voicenet.com>, Ed@eSquare.com
- says...
- >
- >I need to write some code that will open an existing file, find a string
- and then update the string's counter. Nothing here is a problem except
- that I don
- >
- >Thanks & if anyone reading this is wondering how I am doing the rest of
- the stuff, just throw some questions at me & I'd be happy to help a
- fellow newbie.
- >
- >Ed.
- >
- >---------
- >example:
- >---------
- >Changing a file containing:
- >
- >duck 15
- >fred 96
- >bob 342
- >
- >to:
- >
- >duck 15
- >fred 97
- >bob 342
-
- /* doit.c
- * When you run this program it prints out each line from
- * the file to "stdout" which you can then redirect to
- * another file to test the changes you have made.
- * This program will work in MSDOS and UNIX as is. It does not
- * read/write the orig file which you could do with random file
- * I/O - which this example does not use.
- *
- * Run from the cmd line like: doit infilename
- */
- #include <stdio.h>
- int main(int argc, char *argv[]) {
- FILE *f;
- char buf[256];
-
- if (argc!=2) return(1);
- f=fopen(argv[1],"rt");
- if (!f) {
- printf("Sorry, can't open %s\n",argv[1]);
- return(1);
- }
- while (fgets(buf,255,f)) {
- if (!strcmp(buf,"fred 96\n"))
- strcpy(buf,"fred 97\n");
- printf("%s",buf);
- }
- fclose(f);
- }
-
- Hope this helps,
- Greg DiGiorgio
-
-